export const baseUrl = 'http://81.69.46.155:8980/dxbm/a';
uni.addInterceptor('request', {
    invoke(args) {
        if (args.url.startsWith(baseUrl)) {
            return;
        }
        args.url = baseUrl + args.url;
    },
    success(res) {
        if (res.statusCode === 200) {
            const sessionId = res.header['sessionid'];
            if (sessionId) {
                uni.setStorageSync('sessionId', sessionId)
            }
        }
    }
})
export async function doRequest(api, method, header, data, dataType, responseType) {
    try {
        const res = await uni.request({
            url: api,
            method: method,
            header: header,
            data: data,
            timeout: 10000,
            dataType: dataType,
            responseType: responseType
        });
        if (res.statusCode === 200) {
            const contentType = res.header['content-type'];
            const resData = res.data;
            if (contentType.startsWith('application/json')) {
                const code = resData.code;
                const message = resData.message;
                if (code === 200) {
                    return resData.data;
                } else {
                    throw new Error(message);
                }
            } else {
                return resData;
            }
        } else if (res.statusCode === 404) {
            throw new Error('接口不存在');
        } else {
            throw new Error('服务异常，请联系管理员');
        }
    } catch (error) {
        console.log('异常', error);
        if (error instanceof Error) {
            throw error;
        }
    }
}
export async function doGet1(api) {
    return doGet(api, {});
}
export async function doGet2(api, data) {
    return doGet(api, {}, data);
}
export async function doGet3(api, header, data) {
    return doGet(api, header, data, 'json', 'text');
}
export async function doGet4(api, header, data, dataType, responseType) {
    return doRequest(api, 'GET', header, data, dataType, responseType);
}
export async function doPost1(api, data) {
    return doPost(api, {
        'Content-Type': 'application/json'
    }, data);
}
export async function doPost2(api, header, data) {
    return doPost(api, header, data, 'json', 'text');
}
export async function doPost3(api, header, data, dataType, responseType) {
    return doRequest(api, 'POST', header, data, dataType, responseType);
}